package org.gruposp2p.proyectokarma.domain;

import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.codehaus.groovy.grails.commons.ApplicationHolder;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

@Entity
@Table(name="PeerRole")
public class PeerRole implements java.io.Serializable {

	private static Logger logger = LoggerFactory.getLogger(PeerRole.class);
	
	private static final long serialVersionUID = 1L;
	
    @Id @GeneratedValue(strategy=IDENTITY)
    @Column(name="id", unique=true, nullable=false)
    private Long id;
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="peerId", nullable=false)
    private Peer user;
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="roleId", nullable=false)
    private Role role;
    private String version;
    
    public PeerRole() { }
    
    public PeerRole(Peer user, Role role) { 
    	this.user = user;
    	this.role = role;
    }
    
    public static PeerRole create(Peer peer, Role role, Boolean flush) {
		PeerRole peerRole = new PeerRole (peer, role);
		Transaction tx = null;
        ApplicationContext context = (ApplicationContext)ApplicationHolder.
        	getApplication().getMainContext();
        SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		Session session = sessionFactory.getCurrentSession();
		tx = session.beginTransaction();
		session.save(peerRole);
		tx.commit();
		return peerRole;
	}

    public static boolean remove(Peer user, Role role, Boolean flush) {
		Transaction tx = null;
        ApplicationContext context = (ApplicationContext)ApplicationHolder.
        	getApplication().getMainContext();
        SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		Session session = sessionFactory.getCurrentSession();
		tx = session.beginTransaction();
		Query query = session.createQuery("DELETE FROM PeerRole WHERE user=:user AND role=:role")
			.setEntity("peer", user)
			.setEntity("role", role);
		int deletedEntities = query.executeUpdate();
		tx.commit();
		return (deletedEntities > 0) ? true : false;
	}

    public static void removeAll(Peer user) {
    	logger.debug("removeAllremoveAllremoveAll");
		Transaction tx = null;
        ApplicationContext context = (ApplicationContext)ApplicationHolder.
        	getApplication().getMainContext();
        SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		Session session = sessionFactory.getCurrentSession();
		tx = session.beginTransaction();
		Query query = session.createQuery("DELETE FROM PeerRole WHERE user=:user")
			.setEntity("user", user);
		query.executeUpdate();
		tx.commit();
	}

    public static void removeAll(Role role) {
		Transaction tx = null;
        ApplicationContext context = (ApplicationContext)ApplicationHolder.
        	getApplication().getMainContext();
        SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		Session session = sessionFactory.getCurrentSession();
		tx = session.beginTransaction();
		Query query = session.createQuery("DELETE FROM PeerRole WHERE role=:role")
			.setEntity("role", role);
		query.executeUpdate();
		tx.commit();
	}
	
	public void setUser(Peer peer) {
		this.user = peer;
	}
	
	public Peer getUser() {
		return user;
	}
	
	public void setRole(Role role) {
		this.role = role;
	}

	public Role getRole() {
		return role;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getVersion() {
		return version;
	}

}